package com.weitu.emergency.controller;


import com.alibaba.fastjson.JSON;
import com.weitu.base.annotaion.SystemLog;
import com.weitu.base.controller.BaseController;
import com.weitu.emergency.entity.EmergencyTeam;
import com.weitu.emergency.entity.TeamAndRegion;
import com.weitu.emergency.entity.TeamAndUser;
import com.weitu.emergency.service.EmergencyTeamService;
import com.weitu.paging.DataGridPagination;
import com.weitu.plugin.PageView;
import com.weitu.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhangqingxi 2017-05-02
 */
@Controller
@RequestMapping("emergency/team")
public class EmergencyTeamController extends BaseController {

    private static final String MODULE_BATH_PATH = BASE_JSP_PATH + "emergency/team";

    private final EmergencyTeamService emergencyTeamService;

    @Autowired
    public EmergencyTeamController(EmergencyTeamService emergencyTeamService) {
        this.emergencyTeamService = emergencyTeamService;
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ModelAndView listView() {

        ModelAndView modelAndView = new ModelAndView();

        modelAndView.setViewName(MODULE_BATH_PATH + "/list");

        return modelAndView;
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public ModelAndView addView() {

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName(MODULE_BATH_PATH + "/add");
        return modelAndView;
    }

    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public ModelAndView editView(String id) {

        EmergencyTeam team = emergencyTeamService.findById(id);

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName(MODULE_BATH_PATH + "/edit");
        modelAndView.addObject("team", team);

        return modelAndView;
    }


    @RequestMapping(value = "/details", method = RequestMethod.GET)
    public ModelAndView detailsView(String id) {

        EmergencyTeam team = emergencyTeamService.findById(id);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName(MODULE_BATH_PATH + "/details");
        modelAndView.addObject("team", team);

        return modelAndView;
    }


    /**
     * 根据主键查询
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("/findById")
    public EmergencyTeam findById(String id) {
        return emergencyTeamService.findById(id);
    }

    @ResponseBody
    @RequestMapping("/findByParam")
    public Object findByParam(HttpServletRequest request) {

        Map<String, Object> paramMap = new HashMap<String, Object>();

        //获取前台参数
        putParametersIntoMap(paramMap);

        Integer totalNum = emergencyTeamService.findByParamCount(paramMap);//获取总数
        DataGridPagination pagination = DataGridPagination.getInstance(request, totalNum);

        PageView pageView = pagination.getPageView();//生成pageView分页对象
        paramMap.put("pageView", pageView);

        List<EmergencyTeam> teamList = emergencyTeamService.findByParam(paramMap);

        return pagination.flushDataGridData(teamList);
    }


    @ResponseBody
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @SystemLog(module = "应急队伍_基本信息", methods = "添加应急队伍")
    public AjaxResult add(HttpServletRequest request) {


        Map<String, Object> paramMap = new HashMap<String, Object>();
        //获取前台参数
        getAddBasicFieldMap(paramMap);

        String id=paramMap.get("id").toString();

        //领导
        String  leaderUserJson= request.getParameter("leaderUser");
        List<TeamAndUser> leaderUser = JSON.parseArray(leaderUserJson, TeamAndUser.class);
        //职能
        String  functionalUserJson= request.getParameter("functionalUser");
        List<TeamAndUser> functionalUser = JSON.parseArray(functionalUserJson, TeamAndUser.class);
        //基层
        String  basicLevelUserJson= request.getParameter("basicLevelUser");
        List<TeamAndUser> basicLevelUser = JSON.parseArray(basicLevelUserJson, TeamAndUser.class);

        List<TeamAndUser> teamAndUserList=new ArrayList<TeamAndUser>();
        teamAndUserList.addAll(leaderUser);
        teamAndUserList.addAll(functionalUser);
        teamAndUserList.addAll(basicLevelUser);
        for (TeamAndUser teamAndUser :teamAndUserList){
            teamAndUser.setEmergencyTeamId(id);
        }

        //危险区域
        String teamAndRegionJson = request.getParameter("teamAndRegion");
        List<TeamAndRegion> teamAndRegionList = JSON.parseArray(teamAndRegionJson, TeamAndRegion.class);

        for (TeamAndRegion teamAndRegion:teamAndRegionList){
            teamAndRegion.setEmergencyTeamId(id);
        }
        Integer result = emergencyTeamService.add(paramMap,teamAndUserList,teamAndRegionList);

        return resultAjaxResult(result);
    }

    @ResponseBody
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @SystemLog(module = "应急队伍_基本信息", methods = "编辑应急队伍")
    public AjaxResult edit(HttpServletRequest request) {

        Map<String, Object> paramMap = new HashMap<String, Object>();
        //获取前台参数
        getEditBasicFieldMap(paramMap);

        String id=paramMap.get("id").toString();

        //领导
        String  leaderUserJson= request.getParameter("leaderUser");
        List<TeamAndUser> leaderUser = JSON.parseArray(leaderUserJson, TeamAndUser.class);
        //职能
        String  functionalUserJson= request.getParameter("functionalUser");
        List<TeamAndUser> functionalUser = JSON.parseArray(functionalUserJson, TeamAndUser.class);
        //基层
        String  basicLevelUserJson= request.getParameter("basicLevelUser");
        List<TeamAndUser> basicLevelUser = JSON.parseArray(basicLevelUserJson, TeamAndUser.class);

        List<TeamAndUser> teamAndUserList=new ArrayList<TeamAndUser>();
        teamAndUserList.addAll(leaderUser);
        teamAndUserList.addAll(functionalUser);
        teamAndUserList.addAll(basicLevelUser);
        for (TeamAndUser teamAndUser :teamAndUserList){
            teamAndUser.setEmergencyTeamId(id);
        }

        //危险区域
        String teamAndRegionJson = request.getParameter("teamAndRegion");
        List<TeamAndRegion> teamAndRegionList = JSON.parseArray(teamAndRegionJson, TeamAndRegion.class);

        for (TeamAndRegion teamAndRegion:teamAndRegionList){
            teamAndRegion.setEmergencyTeamId(id);
        }
        Integer result = emergencyTeamService.updateById(paramMap,teamAndUserList,teamAndRegionList,id);

        return resultAjaxResult(result);
    }

    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @SystemLog(module = "应急队伍_基本信息", methods = "删除应急队伍")
    public AjaxResult delete() {

        Map<String, Object> paramMap = new HashMap<String, Object>();
        //获取前台参数
        getEditBasicFieldMap(paramMap);

        Integer result = emergencyTeamService.updateById(paramMap);

        return resultAjaxResult(result);
    }


    /**
     * 处理返回参数
     *
     * @param result 返回值
     * @return 返回值
     */
    private AjaxResult resultAjaxResult(Integer result) {
        AjaxResult ajaxResult = new AjaxResult();
        if (result > 0) {
            ajaxResult.setSuccess(true);
            ajaxResult.setMessage("操作成功");
        }
        return ajaxResult;
    }


}
